

public class demo5 {

    public volatile static int count=1;
    public static void main(String[] args) {
        Object loc=new Object();
        Thread t1=new Thread(()->{
            for(int i=1;i<=10;i++){
                    synchronized (loc){
                        while(count%3!=1) {//这里不能使用if,假如B线程结束后，AC同时争夺线程，A先抢到，但是会先进行一次if判断，然后条件
                            //不满足阻塞。C线程满足if,最后notify，但是此时A已经执行过一次IF了，无法再执行If，所以无法正常进行判断并输出A
                            try {
                                loc.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                            System.out.print("A");
                            count++;
                            loc.notifyAll();
                        }

                    }



        });
        Thread t2=new Thread(()->{
            for(int i=1;i<=10;i++){
                synchronized (loc){
                    while(count%3!=2) {
                        try {
                            loc.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                        System.out.print("B");
                        count++;
                        loc.notifyAll();
                    }
            }
        });
        Thread t3=new Thread(()->{
            for(int i=1;i<=10;i++){
                synchronized (loc){
                    while(count%3!=0) {
                        try {
                            loc.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                        System.out.println("C");
                        count++;
                        loc.notifyAll();
                    }
            }
        });
        t1.start();
        t2.start();
        t3.start();


    }
    public static void main1(String[] args) throws InterruptedException {
        Object loc1=new Object();
        Object loc2=new Object();
        Object loc3=new Object();

        Thread t1=new Thread(()-> {
            try{
                for(int i=0;i<10;i++){
                    synchronized (loc1){
                        loc1.wait();
                    }
                    System.out.print("A");
                    synchronized (loc2){
                        loc2.notify();
                    }

                }
            }
            catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread t2=new Thread(()-> {
            try{
                for(int i=0;i<10;i++){
                    synchronized (loc2){
                        loc2.wait();
                    }
                    System.out.print("B");
                    synchronized (loc3){
                        loc3.notify();
                    }

                }
            }
            catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread t3=new Thread(()-> {
           try{
               for(int i=0;i<10;i++){
                   synchronized (loc3){
                       loc3.wait();
                   }
                   System.out.println("C");
                   synchronized (loc1){
                       loc1.notify();
                   }

               }
           }
           catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);
        synchronized (loc1){
            loc1.notify();
        }

    }

}
